home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / answers / perspective.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  6.9 KB  |  293 lines

  1. /*
  2.  * Copyright 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* perspective.c - open a window and clear the background.
  19.  *    Set up callbacks to handle keyboard input.
  20.  *      Model some objects.  Use perspective projection.
  21.  *
  22.  *    F1 key            - print help information
  23.  *    SPACE key        - generates a random background color
  24.  *    <s> key            - toggle smooth/flat shading
  25.  *    Escape Key        - exit program
  26.  */
  27. #include <GL/gl.h>
  28. #include <GL/glu.h>
  29. #include <GL/glut.h>
  30.  
  31. #include <stdio.h>
  32. #include <math.h>
  33.  
  34. /* Function Prototypes */
  35.  
  36. GLvoid initgfx( GLvoid );
  37. GLvoid keyboard( GLubyte, GLint, GLint );
  38. GLvoid specialkeys( GLint, GLint, GLint );
  39. GLvoid drawScene( GLvoid );
  40.  
  41. void checkError( char * );
  42. void printHelp( char * );
  43.  
  44. /* Global Variables */
  45.  
  46. static char *progname; 
  47.  
  48. /* Global Definitions */
  49.  
  50. #define KEY_ESC    27    /* ascii value for the escape key */
  51.  
  52. void
  53. main( int argc, char *argv[] )
  54. {
  55.     GLsizei width, height;
  56.  
  57.     glutInit( &argc, argv );
  58.  
  59.     /* create a window that is 1/4 the size of the screen,
  60.      * and position it in the middle of the screen.
  61.      */
  62.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  63.     height = glutGet( GLUT_SCREEN_HEIGHT );
  64.     glutInitWindowPosition( width / 4, height / 4 );
  65.     glutInitWindowSize( width / 2, height / 2 );
  66.     glutInitDisplayMode( GLUT_RGBA );
  67.     glutCreateWindow( argv[0] );
  68.  
  69.     initgfx();
  70.  
  71.     glutKeyboardFunc( keyboard );
  72.     glutSpecialFunc( specialkeys );
  73.     glutDisplayFunc( drawScene ); 
  74.  
  75.     progname = argv[0];
  76.  
  77.     printHelp( progname );
  78.  
  79.     glutMainLoop();
  80. }
  81.  
  82. void
  83. printHelp( char *progname )
  84. {
  85.     fprintf(stdout, 
  86.         "\n%s - model some objects using a perspective projection\n\n"
  87.         "F1 key        - print help information\n"
  88.         "SPACE Key    - generates a random background color\n"
  89.         "<s> key        - toggle smooth/flat shading\n"
  90.         "Escape Key    - exit the program\n\n",
  91.         progname);
  92. }
  93.  
  94. GLvoid
  95. initgfx( GLvoid )
  96. {
  97.     /* set clear color to blue */
  98.     glClearColor( 0.0, 0.0, 1.0, 1.0 );
  99.  
  100.     glMatrixMode( GL_PROJECTION );
  101.     gluPerspective( 45.0, 1.0, 3.0, 7.0 );
  102.     glMatrixMode( GL_MODELVIEW );
  103. }
  104.  
  105. void 
  106. checkError( char *label )
  107. {
  108.     GLenum error;
  109.     while ( (error = glGetError()) != GL_NO_ERROR )
  110.         printf( "%s: %s\n", label, gluErrorString(error) );
  111. }
  112.  
  113. GLvoid 
  114. keyboard( GLubyte key, GLint x, GLint y )
  115. {
  116.     static GLboolean flat = GL_FALSE;
  117.  
  118.     switch (key) {
  119.     case ' ':    /* SPACE key */
  120.         /* generate a random background color */
  121.         glClearColor( drand48(), drand48(), drand48(), 1.0 ); 
  122.         glutPostRedisplay();
  123.         break;
  124.     case 's':    /* s key */
  125.         /* toggle between smooth and flat shading */
  126.         flat = !flat;
  127.         if (flat)
  128.             glShadeModel( GL_FLAT );
  129.         else 
  130.             glShadeModel( GL_SMOOTH );
  131.         glutPostRedisplay();
  132.         break;
  133.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  134.         exit(0);
  135.     }
  136. }
  137.  
  138. GLvoid 
  139. specialkeys( GLint key, GLint x, GLint y )
  140. {
  141.     switch (key) {
  142.     case GLUT_KEY_F1:    /* Function key #1 */
  143.         /* print help information */
  144.         printHelp( progname );
  145.         break;
  146.     }
  147. }
  148.  
  149. GLvoid
  150. drawScene( GLvoid )
  151. {
  152.     static GLfloat red[] = { 1.0, 0.0, 0.0 };
  153.     static GLfloat yellow[] = { 1.0, 1.0, 0.0 };
  154.     static GLfloat blue[] = { 0.0, 0.0, 1.0 };
  155.     static GLfloat green[] = { 0.0, 1.0, 0.0 };
  156.     static GLfloat darkgreen[] = { 0.0, 0.25, 0.0 };
  157.  
  158.     /* left window */
  159.     static GLfloat v0[] = { -0.4, 0.8 };
  160.     static GLfloat v1[] = { -0.4, 0.4 };
  161.     static GLfloat v2[] = { -0.3, 0.8 };
  162.     static GLfloat v3[] = { -0.3, 0.4 };
  163.     static GLfloat v4[] = { -0.2, 0.8 };
  164.     static GLfloat v5[] = { -0.2, 0.4 };
  165.  
  166.     /* right window */
  167.     static GLfloat v6[] = { 0.2, 0.8 };
  168.     static GLfloat v7[] = { 0.2, 0.4 };
  169.     static GLfloat v8[] = { 0.3, 0.8 };
  170.     static GLfloat v9[] = { 0.3, 0.4 };
  171.     static GLfloat v10[] = { 0.4, 0.8 };
  172.     static GLfloat v11[] = { 0.4, 0.4 };
  173.  
  174.     glClear( GL_COLOR_BUFFER_BIT );
  175.  
  176.     glPushMatrix();
  177.  
  178.     /* move inside the viewing volume */
  179.     glTranslatef( 0.0, 0.0, -4.0 );
  180.  
  181.     /* draw the ground in different shades of green */
  182.     glBegin( GL_POLYGON );
  183.         glColor3fv( green );
  184.         glVertex2f( -2.0, -2.0 );
  185.         glVertex2f( 2.0, -2.0 );
  186.         glColor3fv( darkgreen );
  187.         glVertex2f( 2.0, 0.0 );
  188.         glVertex2f( -2.0, 0.0 );
  189.     glEnd();
  190.  
  191.     /* draw the house */
  192.     glColor3f( 1.0, 1.0, 1.0 ); /* white */
  193.     glRectf( -0.5, -0.5, 0.5, 1.0 );
  194.  
  195.     /* draw the door */
  196.     glColor3f( 0.5, 0.2, 0.1 ); /* brown */
  197.     glRectf( -0.2, -0.5, 0.2, 0.2 );
  198.  
  199.     /* draw the roof */
  200.     glBegin( GL_TRIANGLES );
  201.         glColor3f( 0.0, 0.0, 0.0 ); /* black */
  202.         glVertex2f( -0.5, 1.0 );
  203.         glVertex2f( 0.5, 1.0 );
  204.         glVertex2f( 0.0, 1.5 );
  205.     glEnd();
  206.  
  207.     /* draw 2 quadrilateral strip to make a window */
  208.     glBegin( GL_QUAD_STRIP );
  209.         glColor3f( 1.0, 0.0, 0.0 );
  210.         glVertex2fv (v0);
  211.         glColor3f( 0.9, 0.0, 1.0 );
  212.         glVertex2fv (v1);
  213.         glColor3f( 0.8, 0.1, 0.0 );
  214.         glVertex2fv (v2);
  215.         glColor3f( 0.7, 0.2, 1.0 );
  216.         glVertex2fv (v3);
  217.         glColor3f( 0.6, 0.3, 0.0 );
  218.         glVertex2fv (v4);
  219.         glColor3f( 0.5, 0.4, 1.0 );
  220.         glVertex2fv (v5);
  221.         glColor3f( 0.4, 0.5, 0.0 );
  222.     glEnd();
  223.  
  224.     /* draw 2 quadrilateral strip to make a window */
  225.     glBegin( GL_QUAD_STRIP );
  226.         glVertex2fv (v6);
  227.         glColor3f( 0.3, 0.6, 1.0 );
  228.         glVertex2fv (v7);
  229.         glColor3f( 0.2, 0.7, 0.0 );
  230.         glVertex2fv (v8);
  231.         glColor3f( 0.1, 0.8, 1.0 );
  232.         glVertex2fv (v9);
  233.         glColor3f( 0.0, 0.9, 0.0 );
  234.         glVertex2fv (v10);
  235.         glColor3f( 0.0, 1.0, 1.0 );
  236.         glVertex2fv (v11);
  237.     glEnd();
  238.  
  239.     /* Draw a triangle fan for the sun */
  240.     glBegin( GL_TRIANGLE_FAN );
  241.       glColor3f( 1.0, 1.0, 1.0 );
  242.       glVertex2f( 1.5, 1.5 );
  243.       glColor3fv( yellow );
  244.       glVertex2f( 1.5, 1.3 );
  245.       glVertex2f( 1.7, 1.4 );
  246.       glVertex2f( 1.7, 1.6 );
  247.       glVertex2f( 1.5, 1.7 );
  248.       glVertex2f( 1.3, 1.6 );
  249.       glVertex2f( 1.3, 1.4 );
  250.       glVertex2f( 1.5, 1.3 );
  251.     glEnd();
  252.  
  253.     /* draw a tulip in the foreground */
  254.  
  255.     glTranslatef( 1.0, -1.0, 1.0 );
  256.  
  257.     /* draw the stem with 2 leaves */
  258.  
  259.     glColor3fv( darkgreen );
  260.     glBegin( GL_LINES );
  261.         glVertex2f( 0.0, 0.0 );         /* stem */
  262.         glVertex2f( 0.0, 0.25 );
  263.         glVertex2f( 0.0, 0.1 );         /* leaf */
  264.         glVertex2f( 0.05, 0.15 ); 
  265.         glVertex2f( 0.0, 0.05 );     /* leaf */
  266.         glVertex2f( -0.05, 0.2 );
  267.     glEnd();
  268.  
  269.     /* move to the top of the stem */
  270.  
  271.     glTranslatef( 0.0, 0.25, 0.0 );
  272.  
  273.     /* use a triangle fan for the flower head */
  274.  
  275.     glBegin( GL_TRIANGLE_FAN );
  276.       glColor3f( 1.0, 1.0, 1.0 );
  277.       glVertex2f( 0.0, 0.0 );
  278.       glColor3f( 1.0, 0.0, 0.0 );
  279.       glVertex2f( 0.0, -0.03 );
  280.       glVertex2f( 0.03, -0.02 );
  281.       glVertex2f( 0.03, 0.02 );
  282.       glVertex2f( 0.0, 0.03 );
  283.       glVertex2f( -0.03, 0.02 );
  284.       glVertex2f( -0.03, -0.02 );
  285.       glVertex2f( 0.0, -0.03 );
  286.     glEnd();
  287.  
  288.     glPopMatrix();
  289.  
  290.     checkError( "drawScene" );
  291.     glFlush();
  292. }
  293.